package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.dto.role.RolePage;
import com.abc.cloud.dto.user.UserAndAllRole;
import com.abc.cloud.entity.MenuEntity;
import com.abc.cloud.entity.PermsEntity;
import com.abc.cloud.vo.role.RoleSave;
import com.abc.cloud.vo.role.RoleUpdate;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.RoleEntity;
import com.abc.cloud.service.RoleService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;



/**
 * 角色表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-27 13:09:54
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = roleService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		RoleEntity role = roleService.getById(id);

        return Result.success(role, "role");
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result<RoleEntity> saveRoleInfo(@RequestBody RoleSave roleSave){
        RoleEntity tbRole = roleService.saveRoleInfo(roleSave);
        if(tbRole == null){
            Result.fail(null, "添加角色失败");
        }
        return Result.success(tbRole, "添加角色");
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody RoleEntity role){
		roleService.updateById(role);

        return Result.success(null, "操作成功");
    }

    /**
     * 删除/恢复 角色
     */
    @PutMapping("/updateStatus/{status}")
    public Result delete(@RequestBody Long[] ids,
                         @PathVariable("status") Integer status){
		roleService.updateStatusByIds(Arrays.asList(ids), status);

        return Result.success(null, "操作成功");
    }

    @GetMapping("/getRoleInfo")
    public Result<RoleEntity> getRoleInfo(@RequestParam("roleId") Long roleId){
        RoleEntity tbRole = roleService.getRoleInfo(roleId);
        if(tbRole == null){
            Result.fail(null, "查询角色失败");
        }
        return Result.success(tbRole, "查询角色成功");
    }

    @PutMapping("/updateRoleInfo")
    public Result<RoleEntity> updateRoleInfo(@RequestBody RoleUpdate roleUpdate){
        RoleEntity tbRole = roleService.updateRoleInfo(roleUpdate);
        if(tbRole == null){
            Result.fail(null, "修改角色失败");
        }
        return Result.success(tbRole, "修改角色成功");
    }

    @DeleteMapping("/deleteRoleInfo/{roleId}")
    public Result<Long> deleteRoleInfo(@PathVariable("roleId") Long roleId){
        Boolean i = roleService.deleteRoleInfo(roleId);
        if(!i){
            Result.fail(null, "删除角色失败");
        }
        return Result.success(roleId, "删除角色成功");
    }

    @GetMapping("/getRoleList")
    public Result<List<RoleEntity>> getRoleList(){
        return Result.success(roleService.getRoleList(), "获取角色列表成功");
    }

    @GetMapping("/getRolePage")
    public Result<RolePage> getRolePage(@RequestParam(name = "currentPag", defaultValue = "1") Integer currentPag,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(name = "content", required = false) String content){
        RolePage rolePag = roleService.getRolePage(currentPag, pageSize, content);
        return Result.success(rolePag, "获取角色分页成功");
    }

    @GetMapping("/getRoleMenuList")
    public Result<List<MenuEntity>> getRoleMenuList(@RequestParam("roleId") Long roleId){
        List<MenuEntity> tbAdminMenuList = roleService.getRoleMenuList(roleId);
        return Result.success(tbAdminMenuList, "获取角色后台菜单成功");
    }

    @PutMapping("/updateRoleMenu/{roleId}")
    public Result<List<Long>> updateRoleMenu(@RequestBody List<Long> adminMenuIds,
                                             @PathVariable("roleId") Long roleId){
        Boolean update = roleService.updateRoleMenu(adminMenuIds, roleId);
        if(!update){
            return Result.fail(null, "修改角色后台菜单出错");
        }
        return Result.success(adminMenuIds, "修改角色后台菜单成功");
    }

    @GetMapping("/getRolePermsList")
    public Result<List<PermsEntity>> getRolePermsList(@RequestParam("roleId") Long roleId){
        List<PermsEntity> tbMenus = roleService.getRolePermsList(roleId);
        return Result.success(tbMenus, "查询角色拥有的权限菜单成功");
    }

    @PutMapping("/updateRolePerms/{roleId}")
    public Result<List<Long>> updateRolePerms(@RequestBody List<Long> menuIds,
                                              @PathVariable("roleId") Long roleId){
        Boolean update = roleService.updateRolePerms(menuIds, roleId);
        if(!update){
            return Result.fail(null, "修改角色权限出错");
        }
        return Result.success(menuIds, "修改角色权限成功");
    }

    @ApiOperation("获取用户角色和所有的角色")
    @GetMapping("/admin/getUserAndAllRole")
    public Result<UserAndAllRole> getUserAndAllRole(@RequestParam("username") String username){
        UserAndAllRole userRoleAndAll = roleService.getUserRoleAndAll(username);
        return Result.success(userRoleAndAll, "获取用户身份和全部身份成功");
    }


}
